package com.xiaoshou.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import com.xiaoshou.pojo.Answers;
import com.xiaoshou.pojo.Question;
import com.xiaoshou.pojo.User;

@Controller
@Results({
	@Result(name="saveAnswersBySuccess",location="oneQuestion.action",type="redirectAction",params={"questionId","${question.id}"}),
	@Result(name="toEditAnswerPage",location="editAnswer.jsp",type="dispatcher",params={"answers","${answers}"}),
	@Result(name="updateAnswersSuccess",location="oneQuestion.action",type="redirectAction",params={"questionId","${questionId}"}),
	@Result(name="deleteAnswersSuccess",location="oneQuestion.action",type="redirectAction",params={"questionId","${questionId}"})
	/*@Result(name="saveAnswersSuccessForRedirect",location="saveAnswerSuccessRedirectAction.action",type="redirectAction",params={"questionId","${question.id}"})*/
})
public class AnswerAction extends BaseAction{
//dispatcher
	private static final long serialVersionUID = 1L;
	//E:\Workspaces\eclipse\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\xiaoshou\files
	private Question question;
	private Answers answers;
	private User user;
	
	private String content;
	private String questionId;
	private String answerId;

	@Override
	public String execute() throws Exception {
		return null;
	}
	
	@Action("deleteAnswers")
	public String deleteAnswers(){
		questionId = getAnswersService().getAnswersDao().findById(answerId).getQuestion().getId();
		getAnswersService().deleteAnswerById(answerId);
		return "deleteAnswersSuccess";
	}
	
	

	@Action("updateAnswer")
	public String updateAnswer(){
		getAnswersService().updateAnswers(answers);
		user = (User) getFromSession("user");
		answers = getAnswersService().getAnswersDao().findById(answers.getId());//修改后的answer
		question = answers.getQuestion();
		questionId = question.getId();
		
		return "updateAnswersSuccess";
	}
	
	@Action("editAnswer")
	public String toEditAnswer(){//toEditAnswerPage
		answers = getAnswersService().getAnswersDao().findById(answerId);
		return "toEditAnswerPage";
	}
	
	@Action("saveAnswersBy")
	public String save(){
		user = (User) getFromSession("user");
		question = getQuestionService().getQuestionDao().findById(questionId);
		answers = new Answers();
		answers.setContent(content);
		getAnswersService().saveNewAnswers(answers,question,user);
		questionId = question.getId();
		question = getQuestionService().getQuestionDao().findById(questionId);
		
		//return "saveAnswersSuccessForRedirect";
		return "saveAnswersBySuccess";
	}
	//重定向跳转
	/*@Action("saveAnswerSuccessRedirectAction")
	public String gotoSaveAnswer(){
		return "saveAnswersSuccess";
	}*/

	//set	get方法
	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public Answers getAnswers() {
		return answers;
	}

	public void setAnswers(Answers answers) {
		this.answers = answers;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getQuestionId() {
		return questionId;
	}

	public void setQuestionId(String questionId) {
		this.questionId = questionId;
	}

	public String getAnswerId() {
		return answerId;
	}

	public void setAnswerId(String answerId) {
		this.answerId = answerId;
	}

}
